package de.koller.balance.scene.planet;

import de.koller.aframe.util.math.AIntersectionUtil;
import de.koller.aframe.util.math.FunctionPreview;
import de.koller.aframe.util.math.IFunction;
import de.koller.aframe.util.math.IFunction2D;
import de.koller.oceanFramework.math.RVec2;
import java.awt.Point;
import java.util.Arrays;

public class AtmosphereBounds {
	public static final float ATMOSPHERE_HEIGHT = 0.2f;

	private static class DensityOfGround implements IFunction {
		@Override public float getValue( float x ) {
			if( x <= 0 || x > ATMOSPHERE_HEIGHT )
				return 0;
			
			float dx = ( x * (1f / ATMOSPHERE_HEIGHT) ) - 1;
			return dx*dx;
		}
	}
	
	private static class DensityRel implements IFunction2D {
		private final DensityOfGround density = new DensityOfGround();
		
		@Override public float getValue( float x, float y ) {
			float d = (float)Point.distance( x, y, 0, 0 );
			d -= 1f;
			return density.getValue( d );
		}
	}
	
	private static class LightDis implements IFunction2D {
		public static final float DERIVE_W = 0.0001f;
		private final DensityRel relDens = new DensityRel();
		
		@Override public float getValue( float x, float y ) {
			float d = relDens.getValue( x, y );
			if( d == 0 )
				return 0;
			
			float j = 0;
			for( float i = 0; i < x; i += DERIVE_W )
				j += relDens.getValue( i, y );
			return j;
		}		
	}
	
	private static class SizeDis implements IFunction {
		@Override public float getValue( float x ) {
			RVec2 la = new RVec2( 0, 1f );
			RVec2 lb = new RVec2( (float)Math.cos( x ), (float)Math.sin( x ) + 1f );
			
			RVec2[] i = AIntersectionUtil.lineCircle2D( la, lb, 1f + ATMOSPHERE_HEIGHT );
			System.out.println( Arrays.toString( i ) );
			return Math.min(	RVec2.difference( la, i[0] ).getLength(), 
								RVec2.difference( la, i[1] ).getLength() );
		}
	}
	
	public static void main( String[] args ) {
		FunctionPreview.dontscale( -0.1f );
		FunctionPreview.showFunction( -(float)Math.PI, (float)Math.PI, new SizeDis() );
		
//		FunctionPreview.showFunction( -.1f, ATMOSPHERE_HEIGHT + .1f, new DensityOfGround() );
//		
//		Function3DPreview.COLORS = new int[] { 0xff000000, 0xffffffff };
//		Function3DPreview.showFunction( -1.5f, -1.5f, 3, 3, new DensityRel() );
//		
//		Function3DPreview.HIGH_CALC = true;
//		Function3DPreview.COLORS = new int[] { 0xff000000, 0xff153cd0, 0xffd41101 };
//		Function3DPreview.showFunction( -1.5f, -1.5f, 3, 3, new LightDis() );
	}
	
}
